

interface Array {
	add( item: any ): Array;
	binaryIndexOf(item: any): number;
	contains(lookingFor: any): bool;
	firstOrDefault(predicate?: (item:any)=> bool): any;
}

Array.prototype.add = function (item:any) {
	var a: Array;
	a = <Array>this;
	a.push( item );
	return a;
}

Array.prototype.binaryIndexOf = function(item: any): number {
    var vector: Array;
    vector = <Array>this;

    if (vector.length) {
        var start = 0;
        var end = vector.length - 1;

        while (start <= end) {
            var middle = Math.floor((end - start) / 2) + start;

            if( vector[middle] == item)
                return item;
            else if ( vector[middle] < item )
                start = middle + 1;
            else // if(vector[middle] > item
                end = middle - 1;
        }
    }
    return -1;
}

Array.prototype.contains = function (lookingFor: any): bool {
    var a: Array;
    a = <Array>this;

    for (var index = 0; index < a.length;index++)
        if(a[index] === lookingFor)
            return true;

    return false;
}

Array.prototype.firstOrDefault = function (predicate?: (item: any) =>bool) {
    var a: Array;
    a = <Array>this;

    if (predicate && typeof predicate === 'function') {
        for (var index = 0; index < a.length; index++)
            if (predicate(a[index]))
                return a[index];
    }
    else if (a.length > 0)
        return a[0];

    return null;
}



interface Date {
    getMillisTotal(): number;
    // http://code.google.com/p/datejs/wiki/APIDocumentation
}

Date.prototype.getMillisTotal = function (): number {
    var d: Date;
    d = <Date>this;
    
    var total = 0;

    total += d.getMilliseconds();
    total += d.getSeconds() * 1000;
    total += d.getMinutes() * 60 * 1000;
    total += d.getHours() * 60 * 60 * 1000;

    return total;
}
